package com.tegtech.starter.security.log.service;

import java.util.Date;
import java.util.TimerTask;

import com.tegtech.common.utils.spring.SpringUtils;
import com.tegtech.module.auth.domain.LoginAgent;
import com.tegtech.starter.manager.AsyncFactory;
import com.tegtech.starter.security.log.domain.SysLoginLog;
import com.tegtech.starter.security.log.mapper.LoginLogRecordMapper;

/**
 * 产生登录日志记录任务
 * 
 * @author Jun
 */
public class LoginLogTaskService {
	
	/**
	 * 记录登录信息
	 * 
	 * @param username 用户名
	 * @param status   状态
	 * @param message  消息
	 * @return 任务task
	 */
	public static TimerTask record(final LoginAgent loginAgent, final String username, final String status, final String message) {
		return AsyncFactory.create(() -> {
			// 封装对象
			SysLoginLog sysLoginLog = new SysLoginLog();
			sysLoginLog.setUserName(username);
			sysLoginLog.setIpaddr(loginAgent.getIpaddr());
			sysLoginLog.setLoginLocation(loginAgent.getLoginLocation());
			sysLoginLog.setBrowser(loginAgent.getBrowser());
			sysLoginLog.setOs(loginAgent.getOs());
			sysLoginLog.setStatus(status);
			sysLoginLog.setMsg(message);
			sysLoginLog.setLoginTime(new Date(loginAgent.getLoginTime()));
			// 插入数据
			SpringUtils.getBean(LoginLogRecordMapper.class).insert(sysLoginLog);
		});
	}

}
